package bug
{
	import asds.map.HashMap;	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.Capabilities;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;
	import utils.load.LoaderManager;
	import utils.load.SLoader;

	/**
	 *  资源管理与加载。
	 * @author 青竹 
	 * 
	 */
	public class ResourcesManager
	{
		private static var  cache:HashMap=new HashMap;
		private static var loadCache:Dictionary=new Dictionary();
		//private static var mark:BitmapData=new Mark(0,0) ; 
		private static var _armyIcon0:BitmapData;//; 
		private static var _armyIcon1:BitmapData;// 
		private static var _armyIcon2:BitmapData;// = new UI_ARMY_ICON_2()  as BitmapData ; 
		
		public function ResourcesManager()
		{
		}
		
		public static function init():void{
			//LoaderManager.eventHandle.addEventListener(Event.COMPLETE,onCompleted);
			LoaderManager.addCallBack(callBack);
		}
		private static function onCompleted(e:Event):void{
			loadCache=new Dictionary();
		}
		private static function onProgress(e:ProgressEvent):void{
			
		}
		private static var soundArg:Array=[];
		private static function callBack():void{
			  var a:Object;
		      while(soundArg.length){
			     a=soundArg.shift();
				 startSound(a.id,a.backFun,a.isPlay,a.data);
			  }
		}
		
		/**
		 * 获取声音。 
		 * @param id
		 * @param backFun
		 * @param data
		 * @return 
		 * 
		 */
		public static function getSound(id:String,backFun:Function=null,isPlay:Boolean=true,data:*=null):void{
			startSound(id,backFun,isPlay,data);
		}
		

		private static function startSound(id:String,backFun:Function=null,isPlay:Boolean=true,data:*=null):void{
			if(id==null || id==""){
				//  throw new Error("试图加载错误数据"+id);
				trace("试图加载错误数据"+id);
				return ;
			}
			
			var sound:Sound;
//			if(Config.resTypeIsSwf){
//				id="swf_"+id;
//			}else{
//				id="png_"+id;
//			}
			
			var key:String=id;
			var index:int=id.lastIndexOf("/");
			var url:String=id;
			//if(/*Config.resTypeIsSwf &&*/ index>0) {
			//	url=id.slice(0,index);
			//	id=id.slice(index+1);
			//}
			
			if(cache.containsKey(key)){
				if(data!=null){
					if(backFun is Function)  backFun(cache.get(key) as Sound,data);
				}else{
					if(backFun is Function)  backFun(cache.get(key) as Sound);
				}
				sound=cache.get(key) as Sound;
				if(isPlay) sound.play();
			}else{
				var r:Sound;
				var o:Class;
				var b:SLoader
				if(!ApplicationDomain.currentDomain.hasDefinition(id)){
					if(loadCache.hasOwnProperty(url)){
						b=loadCache[url] as SLoader;
					}else{
						b=new SLoader();
						loadCache[url]=b;
					}
					var lf:LoaderContext=new LoaderContext(false,ApplicationDomain.currentDomain);
					lf.allowCodeImport=true;
					if(lf.hasOwnProperty("allowLoadBytesCodeExecution")) {
						lf.allowLoadBytesCodeExecution=true;
					}
					b.addEventListener(Event.COMPLETE,loaded);
					b.addEventListener(IOErrorEvent.IO_ERROR,onError);
					b.load(new URLRequest(url),lf);
					return;
				}
				o=ApplicationDomain.currentDomain.getDefinition(id) as Class;
				r=new o();
				cache.put(key,r);
				if(data!=null){
					if(backFun is Function)  backFun(r,data);
				}else{
					if(backFun is Function) backFun(r);
				}
				sound=r;
				if(isPlay) sound.play();
			}
			
			function onError(e:IOErrorEvent):void{
				e.currentTarget.removeEventListener(Event.COMPLETE,loaded);
				e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				if(data!=null){
					if(backFun is Function) backFun(null,data);
				}else{
					if(backFun is Function) backFun(null);
				}
				trace("声音失败"+id);
				delete loadCache[url];
			}
			
			function loaded(e:Event):void{
				e.currentTarget.removeEventListener(Event.COMPLETE,loaded);
				e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				if(cache.containsKey(key)){
					if(data!=null){
						if(backFun is Function) backFun(cache.get(key) as Sound,data);
					}else{
						if(backFun is Function) backFun(cache.get(key) as Sound);
					}
					sound=cache.get(key) as Sound;
					return;
				}
				
				if(!ApplicationDomain.currentDomain.hasDefinition(id)){
					trace("id:"+id+" url:"+url,"获取失败..................")
					return
				}
				o=ApplicationDomain.currentDomain.getDefinition(id) as Class;
				r=new o();
				
				cache.put(key,r);
				if(data!=null){
					if(backFun is Function) backFun(r,data);
				}else{
					if(backFun is Function) backFun(r);
				}
				sound=r;
				if(isPlay) sound.play();
				delete loadCache[url];
			}
		}
		/**
		 * 获取一张位图。 
		 * @param id
		 * @param backFun
		 * @param isMask
		 * @param data
		 * @return 
		 * 
		 */
		public static function getBitmap(id:String,backFun:Function=null,data:*=null):Image{
			var key:String=id;
			if(id==null || id=="" || id=="0"){
				trace("试图加载错误数据"+id);
				throw new Error("试图加载错误数据");
			    return null;
			}
			var bitmap:Image=new Image();
		    if(id.slice(0,4)!="http"){
//				if(Config.resTypeIsSwf){
//				    id="swf_"+id;
//				}else{
//				    id="png_"+id;
//				}
			}
			
			var isPng:Boolean=false;
			var index:int=id.lastIndexOf("/");
			var url:String=id;
//			if(Config.resTypeIsSwf && index>0) {
//		    	 url=id.slice(0,index);
//				 isPng= url.slice(url.length-3)!="swf";
//				 if(isPng)
//					url=id;
//			     id=id.slice(index+1);
//			}else{
			     isPng=true;
			//}
			
			
			if(cache.containsKey(key)){
				if(data!=null){	
					if(backFun is Function)  backFun(cache.get(key) as BitmapData,data);
				}else{
					if(backFun is Function)  backFun(cache.get(key) as BitmapData);
				}
				bitmap.bitmapData=cache.get(key) as BitmapData;
			}else{
				var r:BitmapData;
				var o:Class;
				var b:SLoader
				//var sbId:String=id+String(Math.random())
				if(!ApplicationDomain.currentDomain.hasDefinition(id)){
					if(loadCache.hasOwnProperty(url)){
					   b=loadCache[url] as SLoader;
					}else{
					   b=new SLoader();
					   loadCache[url]=b;
					  // trace("url:",source)
					}
					
					var lf:LoaderContext=new LoaderContext(false,ApplicationDomain.currentDomain);
					lf.allowCodeImport=true;
					if(lf.hasOwnProperty("allowLoadBytesCodeExecution")){
						lf.allowLoadBytesCodeExecution=true;
					}
					
                    b.addEventListener(Event.COMPLETE,loaded);
					
					//b.addEventListener(ProgressEvent.PROGRESS,onProgress);
					b.addEventListener(IOErrorEvent.IO_ERROR,onError);
					b.load(new URLRequest(url),lf);
                    trace(url);
					return bitmap;
				}
				o=ApplicationDomain.currentDomain.getDefinition(id) as Class;
				r=new o(0,0);
				cache.put(key,r);
				if(data!=null){
					if(backFun is Function)  backFun(r,data);
				}else{
					if(backFun is Function) backFun(r);
				}
				bitmap.bitmapData=r;
			}
           
		    function onError(e:IOErrorEvent):void{
				e.currentTarget.removeEventListener(Event.COMPLETE,loaded);
				e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				if(data!=null){
					if(backFun is Function) backFun(null,data);
				}else{
					if(backFun is Function) backFun(null);
				}
				delete loadCache[url];
				trace("加载图片失败"+id);
			}
			function loaded(e:Event):void{
			
				e.currentTarget.removeEventListener(Event.COMPLETE,loaded);
				e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				if(cache.containsKey(key)){
					if(data!=null){
						if(backFun is Function) backFun(cache.get(key) as BitmapData,data);
					}else{
						if(backFun is Function) backFun(cache.get(key) as BitmapData);
					}
					bitmap.bitmapData=cache.get(key) as BitmapData;
					return;
				}
				
				if(isPng){
                    r= Bitmap(SLoader(e.currentTarget).contentLoaderInfo.content).bitmapData;
				}else{
					if(!ApplicationDomain.currentDomain.hasDefinition(id)){
						if(Capabilities.isDebugger){
					   		//Alert.showMarquee("id:"+id+" url:"+url+"获取失败(调试)");
							trace("id:"+id+" url:"+url+"获取失败(调试)");
						}
					   return
					}
					o=ApplicationDomain.currentDomain.getDefinition(id) as Class;
					r=new o(0,0);
				}
				cache.put(key,r);
				if(data!=null){
					if(backFun is Function) backFun(r,data);
				}else{
					if(backFun is Function) backFun(r);
				}
				bitmap.bitmapData=r;
				delete loadCache[url];
			}
			return bitmap;
		}
		
		/**
		 * 获取一定义。 
		 * @param id
		 * @param backFun
		 * @param isMask
		 * @param data
		 * @return 
		 * 
		 */
		public static function getClass(id:String,backFun:Function=null,data:*=null,isShowLoading:Boolean=true):void{
			if(id==null || id=="" || id=="0"){
				//  throw new Error("试图加载错误数据"+id);
				trace("试图加载错误数据"+id);
				return ;
			}
//			trace(id,"----------");
//			if(Config.resTypeIsSwf){
//				id="swf_"+id;
//			}else{
//				id="png_"+id;
//			}
			var key:String=id;
			
			var index:int=id.lastIndexOf("/");
			var url:String=id;
			if(index>0) {
				url=id.slice(0,index);
				id=id.slice(index+1);
			}
			
			if(ApplicationDomain.currentDomain.hasDefinition(id)){
				if(data!=null){
					if(backFun is Function)  backFun(ApplicationDomain.currentDomain.getDefinition(id),data);
				}else{
					if(backFun is Function) backFun(ApplicationDomain.currentDomain.getDefinition(id));
				}
			}else{
				var o:Class;
				var b:SLoader;
				if(!ApplicationDomain.currentDomain.hasDefinition(id)){
					if(loadCache.hasOwnProperty(url)){
						b=loadCache[url] as SLoader;
					}else{
						b=new SLoader();
						loadCache[url]=b;
					}
					var lf:LoaderContext=new LoaderContext(false,ApplicationDomain.currentDomain);
					lf.allowCodeImport=true;
					if(lf.hasOwnProperty("allowLoadBytesCodeExecution")) {
						lf.allowLoadBytesCodeExecution=true;
					}
					b.addEventListener(Event.COMPLETE,loaded);
					if(isShowLoading){
					   b.addEventListener(ProgressEvent.PROGRESS,onProgress);
					}
					b.addEventListener(IOErrorEvent.IO_ERROR,onError);
					b.load(new URLRequest(url),lf);
					return;
				}
				o=ApplicationDomain.currentDomain.getDefinition(id) as Class;
				if(data!=null){
					if(backFun is Function) backFun(o,data);
				}else{
					if(backFun is Function) backFun(o);
				}
			}
			
			function onError(e:IOErrorEvent):void{
				e.currentTarget.removeEventListener(Event.COMPLETE,loaded);
				e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				if(data!=null){
					if(backFun is Function) backFun(null,data);
				}else{
					if(backFun is Function) backFun(null);
				}
				trace("加载图片失败"+id);
				delete loadCache[url];
			}
			
			function loaded(e:Event):void{
				e.currentTarget.removeEventListener(Event.COMPLETE,loaded);
				e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				if(ApplicationDomain.currentDomain.hasDefinition(key)){
					if(data!=null){
						if(backFun is Function) backFun(ApplicationDomain.currentDomain.getDefinition(key),data);
					}else{
						if(backFun is Function) backFun(ApplicationDomain.currentDomain.getDefinition(key));
					}
					return;
				}
				
				if(!ApplicationDomain.currentDomain.hasDefinition(id)){
					trace("id:"+id+" url:"+url,"获取失败..................");
					return
				}
				o=ApplicationDomain.currentDomain.getDefinition(id) as Class;
				if(data!=null){
					if(backFun is Function) backFun(o,data);
				}else{
					if(backFun is Function) backFun(o);
				}
				delete loadCache[url];
			}
		}
	}
}